package com.jf.deepseek.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-06-11
 * Time:19:22
 */
@RestController
public class ChatController {
    @Resource
    private ChatClient chatClient;
    private final ChatClient deepSeekChatClient;

    public ChatController(DeepSeekChatModel deepSeekChatModel) {
        this.deepSeekChatClient = ChatClient.builder(deepSeekChatModel).build();
    }

    @GetMapping(value = "/chat", produces = "text/html;charset=UTF-8")
    public Flux<String> chat(@RequestParam(value = "message", defaultValue = "你好") String message) {
        return chatClient.prompt().user(message).stream().content();
    }
    @GetMapping(value = "/generateCode")
    public Flux<String> chat2(@RequestParam(value = "message") String message) {
        UserMessage userMessage = new UserMessage(message);
        Message assistantMessage = DeepSeekAssistantMessage.prefixAssistantMessage("```java\\n");
        Prompt prompt = new Prompt(List.of(userMessage, assistantMessage));
        return deepSeekChatClient.prompt(prompt).stream().content();
    }
}
